/************************************************************************************
FileName : cgi_ctrl.c
Description : Ctrl codes for calling CGI Interface SDK
************************************************************************************/

#include <stdint.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>

#include "cgi_common.h"
#include "cgi_ctrl.h"
#include "alarm_info.h"
#include "crc32.h"
#include "network.h"
#include "settings_manager.h"

//#define CGI_DEBUG
#ifdef CGI_DEBUG
#define DEBUG(fmt, arg...) printf(fmt, ##arg)
#else
#define DEBUG(fmt, arg...)
#endif

int set_video_fmt_2_json(int value);
int set_video_bitrate_2_json(int value);
int set_video_gdrmode_2_json(int value);
int set_video_gop_2_json(int value);
int set_video_mode_2_json(int value);
int set_video_framerate_2_json(int value);
int set_video_height_2_json(int value);
int set_video_width_2_json(int value);
int set_audioswitch_2_json(int value);

static int snd_buff_add_int(unsigned char * buf, int * index, int val)
{
	int i = *index;
	if(i + 4 >= CGI_PACK_MAX_LEN - CGI_PACK_CRC_LEN)
		return 0;

	unsigned int value = (unsigned int)val;
	buf[i++] = (value & 0xFF000000) >> 24;
	buf[i++] = (value & 0x00FF0000) >> 16;
	buf[i++] = (value & 0x0000FF00) >> 8;
	buf[i++] = value & 0x000000FF;
	DEBUG("sndbuf add int: %d\n", value);
	*index = i;
	return 1;
}

static int recv_buff_get_int(unsigned char * buf, int buf_len, int * index, int * val)
{
	int i = *index;
	if(i + 4 > buf_len - CGI_PACK_CRC_LEN)
		return 0;

	unsigned int value = 0;
	value += (unsigned int)buf[i++] << 24;
	value += (unsigned int)buf[i++] << 16;
	value += (unsigned int)buf[i++] << 8;
	value += (unsigned int)buf[i++];
	DEBUG("recvbuf get int: %d\n", value);
	*index = i;
	*val = (int)value;
	return 1;
}


static int snd_buff_add_str(unsigned char * buf, int * index, char * str)
{
	int i = *index;
	int len = strlen(str);
	if(i + 1 + len >= CGI_PACK_MAX_LEN - CGI_PACK_CRC_LEN)
		return 0;

	buf[i++] = len;
	memcpy(buf + i, str, len);
	i += len;
	DEBUG("sndbuf add str: %s\n", str);
	*index = i;
	return 1;
}

static int recv_buff_get_str(unsigned char * buf, int buf_len, int * index, char * str, int str_len)
{
	int i = *index;
	if(i + 1 > buf_len - CGI_PACK_CRC_LEN)
		return 0;
	int len = buf[i++];
	if(i + len > buf_len - CGI_PACK_CRC_LEN || len >= str_len)
		return 0;

	memset(str, '\0', str_len);
	memcpy(str, (char *)buf + i, len);
	i += len;
	DEBUG("recvbuf get str: %s\n", str);
	*index = i;
	return 1;
}


int process_cgi_cmd_proc(unsigned char * buf, int buf_len, int sock, unsigned short cmd)
{
	unsigned char snd_cmd[CGI_PACK_MAX_LEN];
	memset(snd_cmd, '\0', CGI_PACK_MAX_LEN);
	snd_cmd[0] = CGI_PACK_HEAD_1;
	snd_cmd[1] = CGI_PACK_HEAD_2;
	snd_cmd[2] = CGI_PACK_HEAD_3;
	snd_cmd[3] = CGI_PACK_VERSION;

	int snd_index = CGI_PACK_LEAST_LEN - CGI_PACK_CRC_LEN;
	int recv_index = CGI_PACK_LEAST_LEN - CGI_PACK_CRC_LEN;
	int value = 0;
	int value_tmp = 0;
	char rtspaddr[CGI_COMMON_LEN] = { 0 };
	char rtspaddr_tmp[CGI_COMMON_LEN] = { 0 };
	char addr[CGI_IP_LEN] = {0};
	char addr_tmp[CGI_IP_LEN] = {0};
	char passwd[CGI_PASSWORD_LEN] = {0};
	char passwd_tmp[CGI_PASSWORD_LEN] = {0};
	char string[CGI_IP_LEN] = {0};
	char alarm_buf[ALARM_INFO_COL_NUMBER] = {0};
	char ping_buf[ALARM_INFO_COL_NUMBER] = {0};
	char ntp_addr[CGI_COMMON_LEN] = {0};
	char username[CGI_COMMON_LEN] = {0};
	char password[CGI_COMMON_LEN] = {0};
	char webname[CGI_COMMON_LEN] = {0};
	char webpasswd[CGI_COMMON_LEN] = {0};
	char wifiname[CGI_COMMON_LEN] = { 0 };
	char wifiname_tmp[CGI_COMMON_LEN] = { 0 };
	char wifipassword[CGI_COMMON_LEN] = { 0 };
	char wifipassword_tmp[CGI_COMMON_LEN] = { 0 };
	int login_success = 0;
	int ping_success = 0;
	int ret = 1;
	int ret_len = 0;
	int ret_cmd = 0;
	int restart_flag = 0;
	int netintf_ctrl_change_flag = 0;
	int netintf_stream_change_flag = 0;

	int i = 0;
	time_t timer;
	struct tm* t_tm;
	char cgi_date_value[CGI_IP_LEN];
	char cgi_time_value[CGI_IP_LEN];
	int cgi_date[3] = { 0,0,0 };
	int cgi_time[3] = { 0,0,0 };
	char system_date_time[CGI_IP_LEN];
	char *cgi_date_str = NULL;
	
	switch (cmd)
	{
	case CGI_CMD_GET_SYSTEM:
		ret_cmd = CGI_CMD_GET_SYSTEM_RET;
		get_decoder_app_version_setting(string);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, string);
		
		get_decoder_fpga_version_setting(string);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, string);

		get_serial_number_setting(string);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, string);

		get_wifi_switch_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);

		get_wifi_name_setting(wifiname);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, wifiname);

		get_wifi_passwd_setting(wifipassword);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, wifipassword);
		break;

	case CGI_CMD_LOAD_DEFAULT:
		ret_cmd = CGI_CMD_LOAD_DEFAULT_RET;
		//load_default();
		break;

	case CGI_CMD_GET_STATUS:
		ret_cmd = CGI_CMD_GET_STATUS_RET;
		//TODO: get input/output data rate
		//value = get_input_data_rate();
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		value = get_zynqmp_cpu_temp_value();
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		//value = get_stream_info_pmt_pid();
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		//value = get_stream_info_pcr_pid();
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		//value = get_stream_info_video_pid();
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		//value = get_stream_info_audio1_pid();
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);

		//value = get_stream_info_output_format();
		ret &= snd_buff_add_str(snd_cmd, &snd_index, "1080P60");

		//value = get_stream_info_audio_format();
		ret &= snd_buff_add_str(snd_cmd, &snd_index, "LPCM");

		//value = get_stream_info_audio1_rate();
		ret &= snd_buff_add_int(snd_cmd, &snd_index, 128);

		//value = get_stream_info_chroma_format();
		ret &= snd_buff_add_str(snd_cmd, &snd_index, "4:2:0");

		//value = get_stream_info_rate_mode();
		ret &= snd_buff_add_str(snd_cmd, &snd_index, "N/A");

		//value = get_input_source_setting(NULL);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, "IP");
		//value = get_module_state();
		switch (value)
		{
		case 0:
			ret &= snd_buff_add_str(snd_cmd, &snd_index, "Running");
			break;
		case 1:
			ret &= snd_buff_add_str(snd_cmd, &snd_index, "Mismatch");
			break;
		case 2:
		default:
			ret &= snd_buff_add_str(snd_cmd, &snd_index, "Stopped");
			break;
		}
		break;

	case CGI_CMD_GET_CONFIGURE:
		ret_cmd = CGI_CMD_GET_CONFIGURE_RET;
		get_input_source_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		//ip mode
		get_iprecv_mode_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_ip_port_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_ip_multicastaddr_setting(addr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, addr);
		get_ip_unicastaddr_setting(addr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, addr);
		get_rtspaddr_setting(rtspaddr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, rtspaddr);
		//srt mode
		get_srt_mode_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_srt_calleraddr_setting(addr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, addr);
		get_srt_port_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_srt_latency_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_srt_encrpyt_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_srt_password_setting(passwd);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, passwd);
		//VCU param
		get_video_fmt_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_video_gdrmode_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_video_gop_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_video_mode_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_decoder_bitrate_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_video_framerate_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_video_height_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_video_width_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_decoder_audioswitch_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		break;
	
	case CGI_CMD_GET_NETWORK:
		ret_cmd = CGI_CMD_GET_NETWORK_RET;
		get_ip_addressing_setting(&value);
		ret &= snd_buff_add_int(snd_cmd, &snd_index, value);
		get_mac_address_setting(addr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, addr);
		get_stream_ipaddr_setting(addr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, addr);
		get_stream_mask_setting(addr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, addr);
		get_stream_gateway_setting(addr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, addr);

		get_ctrl_mac_address(addr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, addr);
		get_ctrl_ipaddr_setting(addr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, addr);
		get_ctrl_mask_setting(addr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, addr);
		get_ctrl_gateway_setting(addr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, addr);
		get_ping_address_setting(addr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, addr);
		break;
		
	case CGI_CMD_GET_DEVICE:
		ret_cmd = CGI_CMD_GET_DEVICE_RET;
		get_decoder_device_showname_setting(string);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, string);
		break;

	case CGI_CMD_GET_TEMPERATURE:
		ret_cmd = CGI_CMD_GET_TEMPERATURE_RET;
		value = get_zynqmp_cpu_temp_value();
		ret	&= snd_buff_add_int(snd_cmd, &snd_index, value);
		break;

	case CGI_CMD_GET_ALARM_INFO:
		ret_cmd = CGI_CMD_GET_ALARM_INFO_RET;
		for (i = 0; i < ALARM_INFO_LINE_NUMBER; i++)
		{
			memset(alarm_buf, '\0', ALARM_INFO_COL_NUMBER);
			get_alarm_info_buf(i, alarm_buf);
			ret &= snd_buff_add_str(snd_cmd, &snd_index, alarm_buf);
		}
		break;

	case CGI_CMD_GET_PING_ADDR:
		ret_cmd = CGI_CMD_GET_PING_ADDR_RET;
		get_ping_address_setting(addr);
		ret &= snd_buff_add_str(snd_cmd, &snd_index, addr);
		break;
		
	case CGI_CMD_SET_CONFIGURE:
		ret_cmd = CGI_CMD_SET_CONFIGURE_RET;
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);
		if (ret && value != get_input_source_setting(NULL))
		{
			restart_flag |= 1;
			set_input_source_setting(value);
		}
		//ip mode
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);
		if (ret && value != get_iprecv_mode_setting(NULL))
		{
			restart_flag |= 1;
			set_iprecv_mode_setting(value);
		}
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);
		if (ret && value != get_ip_port_setting(NULL))
		{
			restart_flag |= 1;
			set_ip_port_setting(value);
		}
		ret &= recv_buff_get_str(buf, buf_len, &recv_index, addr, CGI_IP_LEN);
		get_ip_multicastaddr_setting(addr_tmp);
		if (ret && strncmp(addr, addr_tmp, CGI_IP_LEN) != 0)
		{
			restart_flag |= 1;
			set_ip_multicastaddr_setting(addr);
		}
		ret &= recv_buff_get_str(buf, buf_len, &recv_index, addr, CGI_IP_LEN);
		get_ip_unicastaddr_setting(addr_tmp);
		if (ret && strncmp(addr, addr_tmp, CGI_IP_LEN) != 0)
		{
			restart_flag |= 1;
			set_ip_unicastaddr_setting(addr);
		}
		ret &= recv_buff_get_str(buf, buf_len, &recv_index, rtspaddr, CGI_COMMON_LEN);
		get_rtspaddr_setting(rtspaddr_tmp);
		if (ret && strncmp(rtspaddr, rtspaddr_tmp, CGI_COMMON_LEN) != 0)
		{
			restart_flag |= 1;
			set_rtspaddr_setting(rtspaddr);
		}
		//srt mode
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);
		if (ret && value != get_srt_mode_setting(NULL))
		{
			restart_flag |= 1;
			set_srt_mode_setting(value);
		}
		ret &= recv_buff_get_str(buf, buf_len, &recv_index, addr, CGI_IP_LEN);
		get_srt_calleraddr_setting(addr_tmp);
		if (ret && strncmp(addr, addr_tmp, CGI_IP_LEN) != 0)
		{
			restart_flag |= 1;
			set_srt_calleraddr_setting(addr);
		}
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);
		if (ret && value != get_srt_port_setting(NULL))
		{
			restart_flag |= 1;
			set_srt_port_setting(value);
		}
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);
		if (ret && value != get_srt_latency_setting(NULL))
		{
			restart_flag |= 1;
			set_srt_latency_setting(value);
		}
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);
		if (ret && value != get_srt_encrpyt_setting(NULL))
		{
			restart_flag |= 1;
			set_srt_encrpyt_setting(value);
		}
		ret &= recv_buff_get_str(buf, buf_len, &recv_index, passwd, CGI_PASSWORD_LEN);
		get_srt_password_setting(passwd_tmp);
		if (ret && strncmp(passwd, passwd_tmp, CGI_PASSWORD_LEN) != 0)
		{
			restart_flag |= 1;
			set_srt_password_setting(passwd);
		}
		//decoder param
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);//fmt
 		if (ret && value != get_video_fmt_setting(NULL))
 		{
 			restart_flag |= 1;
 			set_video_fmt_2_json(value);
 		}
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);//gdrmode
 		if (ret && value != get_video_gdrmode_setting(NULL))
 		{
 			restart_flag |= 1;
			set_video_gdrmode_2_json(value);
 		}
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);//gop
		if (ret && value != get_video_gop_setting(NULL))
		{
			set_video_gop_2_json(value);
			restart_flag |= 1;
		}
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);//mode
		if (ret && value != get_video_mode_setting(NULL))
		{
			set_video_mode_2_json(value);
			restart_flag |= 1;
		}
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);//bitrate
		if (ret && value != get_decoder_bitrate_setting(NULL))
		{
			restart_flag |= 1;
			set_video_bitrate_2_json(value);
		}
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);//framerate
		if (ret && value != get_video_framerate_setting(NULL))
		{
			set_video_framerate_2_json(value);
			restart_flag |= 1;
		}
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);//height
		if (ret && value != get_video_height_setting(NULL))
		{
			set_video_height_2_json(value);
			restart_flag |= 1;
		}
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);//width
		if (ret && value != get_video_width_setting(NULL))
		{
			set_video_width_2_json(value);
			restart_flag |= 1;
		}
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);//audioswitch
		if (ret && value != get_decoder_audioswitch_setting(NULL))
		{
			set_audioswitch_2_json(value);
			restart_flag |= 1;
		}
		if (restart_flag)
		{
			//restart task
		}
		else
		{
			//nothing change
		}
		break;
		
	case CGI_CMD_SET_NETWORK:
		ret_cmd = CGI_CMD_SET_NETWORK_RET;
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);
		if (ret && value != get_ip_addressing_setting(NULL))
		{
			netintf_stream_change_flag = 1;
			set_ip_addressing_setting(value);
		}

		ret &= recv_buff_get_str(buf, buf_len, &recv_index, addr, CGI_IP_LEN);
		get_ctrl_ipaddr_setting(addr_tmp);
		if(ret && strncmp(addr, addr_tmp, CGI_IP_LEN) != 0)
		{
			set_ctrl_ipaddr_setting(addr);
			netintf_ctrl_change_flag = 1;
		}
		ret &= recv_buff_get_str(buf, buf_len, &recv_index, addr, CGI_IP_LEN);
		get_ctrl_mask_setting(addr_tmp);
		if(ret && strncmp(addr, addr_tmp, CGI_IP_LEN) != 0)
		{
			set_ctrl_mask_setting(addr);
			netintf_ctrl_change_flag = 1;
		}
		
		ret &= recv_buff_get_str(buf, buf_len, &recv_index, addr, CGI_IP_LEN);
		get_stream_ipaddr_setting(addr_tmp);
		if(ret && strncmp(addr, addr_tmp, CGI_IP_LEN) != 0)
		{
			set_stream_ipaddr_setting(addr);
			netintf_stream_change_flag = 1;
		}
		ret &= recv_buff_get_str(buf, buf_len, &recv_index, addr, CGI_IP_LEN);
		get_stream_mask_setting(addr_tmp);
		if(ret && strncmp(addr, addr_tmp, CGI_IP_LEN) != 0)
		{
			set_stream_mask_setting(addr);
			netintf_stream_change_flag = 1;
		}
		ret &= recv_buff_get_str(buf, buf_len, &recv_index, addr, CGI_IP_LEN);
		get_stream_gateway_setting(addr_tmp);
		if(ret && strncmp(addr, addr_tmp, CGI_IP_LEN) != 0)
		{
			set_stream_gateway_setting(addr);
			netintf_stream_change_flag = 1;
		}
		//config network if necessary
		if(netintf_ctrl_change_flag)
		{
			network_config(NET_DEV_CRTL);
		}
		if(netintf_stream_change_flag)
		{
			network_config(NET_DEV_STREAM);
		}
		//set_restart_work_flag(1);
		break;

	case CGI_CMD_CLEAR_ALARM_INFO:
		ret_cmd = CGI_CMD_CLEAR_ALARM_INFO_RET;
		set_alarm_info_buf("clear");
		break;
	case CGI_CMD_LOGIN:
		ret_cmd = CGI_CMD_LOGIN_RET;
		ret &= recv_buff_get_str(buf, buf_len, &recv_index, webname, CGI_COMMON_LEN);
		ret &= recv_buff_get_str(buf, buf_len, &recv_index, webpasswd, CGI_COMMON_LEN);
		get_kvm_username_setting(username);
		get_kvm_passwd_setting(password);
		if ((strlen(webname) != strlen(username)) || (strlen(webpasswd) != strlen(password)))
		{
			printf("name passwd error web name %s pswd %s; %s %s\n", webname, webpasswd, username, password);
			login_success = DATETIME_CGI_LOGIN_FAIL;//for test
		}
		else
		{
			if ((memcmp(webname, username, strlen(webname)) == 0) && (memcmp(webpasswd, password, strlen(webpasswd)) == 0))
			{
				login_success = DATETIME_CGI_LOGIN_OK;
			}
			else
			{
				printf("name passwd error\n");
				login_success = DATETIME_CGI_LOGIN_FAIL;//for test
			}
		}
		ret &= snd_buff_add_int(snd_cmd, &snd_index, login_success);
		break;
	case CGI_CMD_APPLY_ACCOUNT:
		ret_cmd = CGI_CMD_APPLY_ACCOUNT_RET;
		ret &= recv_buff_get_str(buf, buf_len, &recv_index, password, CGI_COMMON_LEN);
		set_decoder_password_setting(password);
		break;
	case CGI_CMD_PING_TEST:
		ret_cmd = CGI_CMD_PING_TEST_RET;
		ret &= recv_buff_get_str(buf, buf_len, &recv_index, addr, CGI_COMMON_LEN);
		set_ping_address_setting(addr);
		ping_success = test_ping_target_ip(addr, ping_buf, sizeof(ping_buf));
		ret &= snd_buff_add_str(snd_cmd, &snd_index, ping_buf);
		break;
	case CGI_CMD_APPLY_WIFI:
		ret_cmd = CGI_CMD_APPLY_WIFI_RET;
		ret &= recv_buff_get_int(buf, buf_len, &recv_index, &value);
		if (ret && value != get_wifi_switch_setting(NULL))
		{
			set_wifi_switch_setting(value);
		}

		ret &= recv_buff_get_str(buf, buf_len, &recv_index, wifiname, CGI_COMMON_LEN);
		get_wifi_name_setting(wifiname_tmp);
		if (ret && strncmp(wifiname, wifiname_tmp, CGI_IP_LEN) != 0)
		{
			set_wifi_name_setting(wifiname);
		}

		ret &= recv_buff_get_str(buf, buf_len, &recv_index, wifipassword, CGI_COMMON_LEN);
		get_wifi_passwd_setting(wifipassword_tmp);
		if (ret && strncmp(wifipassword, wifipassword_tmp, CGI_IP_LEN) != 0)
		{
			set_wifi_passwd_setting(wifipassword);
		}
		break;
	default:
		ret_cmd = CGI_CMD_NOT_SUPPORT;
		break;
	}
	ret_len = snd_index + CGI_PACK_CRC_LEN;
	snd_cmd[4] = (ret_len & 0xFF00) >> 8;
	snd_cmd[5] = ret_len & 0x00FF;
	snd_cmd[6] = (ret_cmd & 0xFF00) >> 8;
	snd_cmd[7] = ret_cmd & 0x00FF;
	crc32_calc((char *)snd_cmd, ret_len - 4, (char *)&(snd_cmd[ret_len - 4]));
	if (ret_len != send(sock, snd_cmd, ret_len, 0))
	{
		printf("[CGI] Send data failed!\n");
	}

	if(ret == 0)
	{
		printf("[CGI] Send buff add data failed! Buffer too small? ret_cmd:0x%04x\n", ret_cmd);
	}
	return 0;
}
